package com.ruoyi.teacher.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.dingtalk.pojo.dto.GroupDto;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.teacher.domain.TeacherWorkSchedule;
import com.ruoyi.teacher.service.ITeacherWorkScheduleService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 老师排班Controller
 *
 * @author clid
 * @date 2024-03-25
 */
@RestController
@RequestMapping("/schedule")
public class TeacherWorkScheduleController extends BaseController {
    @Autowired
    private ITeacherWorkScheduleService teacherWorkScheduleService;


    @PostMapping("/batchScheduling")
    public R<Void> batchScheduling(@RequestBody Map<String,String> map) {
        try {
            teacherWorkScheduleService.batchScheduling(map);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 打卡提醒
     *
     * @return {@link R}<{@link Void}>
     */
    @GetMapping("/punchReminder")
    public R<Void> punchReminder() {
        try {
            teacherWorkScheduleService.punchReminder();
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 考勤同步
     *
     * @return {@link R}<{@link Void}>
     */
    @GetMapping("/attendanceSynchronization")
    public R<Void> attendanceSynchronization() {
        try {
            teacherWorkScheduleService.attendanceSynchronization();
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return R.ok();
    }


    @GetMapping("/loadAttendanceSchedule")
    public R<List<GroupDto>> loadAttendanceSchedule() {
        try {
            return R.ok(teacherWorkScheduleService.loadAttendanceSchedule());
        } catch (ApiException e) {
            e.printStackTrace();
            return R.fail("获取考勤组失败");
        }
    }


    /**
     * 查询老师排班列表
     */
    @RequiresPermissions("teacher:schedule:list")
    @GetMapping("/list")
    public TableDataInfo list(TeacherWorkSchedule teacherWorkSchedule) {
        startPage();
        List<TeacherWorkSchedule> list = teacherWorkScheduleService.selectTeacherWorkScheduleList(teacherWorkSchedule);
        return getDataTable(list);
    }

    /**
     * 导出老师排班列表
     */
    @RequiresPermissions("teacher:schedule:export")
    @Log(title = "老师排班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeacherWorkSchedule teacherWorkSchedule) {
        List<TeacherWorkSchedule> list = teacherWorkScheduleService.selectTeacherWorkScheduleList(teacherWorkSchedule);
        ExcelUtil<TeacherWorkSchedule> util = new ExcelUtil<TeacherWorkSchedule>(TeacherWorkSchedule.class);
        util.exportExcel(response, list, "老师排班数据");
    }

    /**
     * 获取老师排班详细信息
     */
    @RequiresPermissions("teacher:schedule:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(teacherWorkScheduleService.selectTeacherWorkScheduleById(id));
    }

    /**
     * 新增老师排班
     */
    @RequiresPermissions("teacher:schedule:add")
    @Log(title = "老师排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeacherWorkSchedule teacherWorkSchedule) {
        try {
            return toAjax(teacherWorkScheduleService.insertTeacherWorkSchedule(teacherWorkSchedule));
        } catch (ApiException e) {
            e.printStackTrace();
            return AjaxResult.error("排班失败");
        }
    }

    /**
     * 修改老师排班
     */
    @RequiresPermissions("teacher:schedule:edit")
    @Log(title = "老师排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeacherWorkSchedule teacherWorkSchedule) {
        return toAjax(teacherWorkScheduleService.updateTeacherWorkSchedule(teacherWorkSchedule));
    }

}
